package com.netease.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.netease.common.HttpUtil;
import com.netease.domain.LoadBalanceItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongle on 2017/2/21.
 */
@Service
public class LoadBalanceService {
    private static String listUrl = "https://open.c.163.com/api/v1/nlb/loadbalancers?offset=%d&limit=%d";

    @Autowired
    private AccessTokenService accessTokenService;

    public List<LoadBalanceItem> getList() throws IOException {
        Map<String, String> header = new HashMap<>();
        header.put("Authorization", "Token "+accessTokenService.getToken());
        String response = HttpUtil.getInstance().commonGet(String.format(listUrl, 0, 50), header);
        ObjectMapper mapper = new ObjectMapper();
        List<LoadBalanceItem> ret = mapper.readValue(response, TypeFactory.defaultInstance().constructCollectionType(List.class, LoadBalanceItem.class));
        return ret;
//        String urlStr = "https://c.163.com/api/v1/win/virtualservers/getServerList?pageNum=1&pageSize=20";
//        Map<String, String> header = new HashMap<>();
//        header.put("Cookie", "isFirst=yes; __da_ntes_utmfc=utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none); _ntes_nuid=eb1f6b1045cf2b2724c0f121bb475d9f; NTESnce-webSI=B476A4D58DEC3CFEC64970919276EE16.10-166-0-216-8010; NICE_SO=1983e52ca2520b63350bda8552dd812f6213708a6384743b; NICE_SO=1983e52ca2520b63350bda8552dd812f6213708a6384743b; urs_t=ni8jDJF5eyEgp-RpaenKOGUg; urs_u=6E/NK1JPJGYj-xK4WBMaPKUjiuVhzptbtH/kkpqYCqODsHLOoyjXbOFhC7pX-mZAxLQvU6JyStZVTX/b4lIjNBrJ1dIKJV-z5guEzi0ktx7vhti2fkYSwZGwR8BIqWbMolnNTdsOC-SV1vn3CCMNcnAEqLdZnyhzSohwY7rhGWLHxd65B6uUygIhcixfr0mdQe1fqvyZgAwMnzJhsE/p24QH8V8KPZrA5NU0XXxKrehRXpZ1O-7IPZlODen3rF3kEGW1gPnyPJBGuLeCIaJMBgiU4VqmPdAOVHgNh8W2gdiECDo1BfNHipK/oGsMuD9JEwR/-V/295gXLkXmsU8n9jMaSZigvgs92SFjgzWpmUU=; P_CLOUD_USER=cnpbimserver@163.com; identity=2165b101-4fa0-49f3-b5b9-3e73b2d3f7ec; Hm_lvt_7bfc014b3c883508fc60c516f8f527f4=1487234475,1487670928; Hm_lpvt_7bfc014b3c883508fc60c516f8f527f4=1488159753; davisit=17; __da_ntes_utma=5463265.1783361343.1482238114.1487900460.1488159762.13; __da_ntes_utmz=5463265.1482238114.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none); LAST_LOGIN_TYPE=1; LAST_LOGIN_ACCOUNT=cn****r@163.com; NETEASE_WDA_UID=138214#|#1478068843678; ACCOUNT_CREATETIME=1478068843678; INVITE_TYPE=\"\"; INVITE_TYPE=\"\"; NICE_S=3cc949e32848e38c67436472f64f7bcab1634b4d6a3976c1b00f3a7c03dde722f79aa19c361002331b30612287cfcfe3eb8a7c830765047a95b1db805330a75bb2eecb336c46c9c79b9df6fe9d5da7d000986b169ce7fdba5ddc3e41834f566ae389d1d1c0a151f26c71ebb902cddc6e6af4e4077212a89ef58789a893f2d251; NICE_S=3cc949e32848e38c67436472f64f7bcab1634b4d6a3976c1b00f3a7c03dde722f79aa19c361002331b30612287cfcfe3eb8a7c830765047a95b1db805330a75bb2eecb336c46c9c79b9df6fe9d5da7d000986b169ce7fdba5ddc3e41834f566ae389d1d1c0a151f26c71ebb902cddc6e6af4e4077212a89ef58789a893f2d251; __utma=5463265.2132477697.1482238112.1488159762.1488159762.21; __utmb=5463265.5.9.1488162729127; __utmc=5463265; __utmz=5463265.1482238112.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)");
//        String response = HttpUtil.getInstance().commonGet(urlStr, header);
//        return null;
    }
}
